﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using LaughingDog.GameObjects;
using Microsoft.Xna.Framework;
using FarseerGames.FarseerPhysics.Factories;
using LaughingDog.Managers;

namespace LaughingDog.Components
{
    public class FixedLinearSpring : Spring
    {
        private FarseerGames.FarseerPhysics.Dynamics.Springs.FixedLinearSpring mFixedLinearSpring = null;

        /// <summary>
        /// Gets or sets the attach point of this spring.
        /// </summary>
        public Vector2 BodyAttachPoint { get { return mFixedLinearSpring.BodyAttachPoint; } set { mFixedLinearSpring.BodyAttachPoint = value; } }

        /// <summary>
        /// Gets or sets the world position of the attach point of this spring.
        /// </summary>
        public Vector2 WorldAttachPoint { get { return mFixedLinearSpring.WorldAttachPoint; } set { mFixedLinearSpring.WorldAttachPoint = value; } }                

        /// <summary>
        /// Gets or sets the length the spring will be when it’s not pulling or pushing.
        /// </summary>
        public Single RestLength { get { return mFixedLinearSpring.RestLength; } set { mFixedLinearSpring.RestLength = value; } }

        /// <summary>
        /// Gets or sets the pull/push of the spring. Think of this as the force of the spring.
        /// </summary>
        public Single SpringConstant { get { return mFixedLinearSpring.SpringConstant; } set { mFixedLinearSpring.SpringConstant = value; } }

        /// <summary>
        /// Gets or sets the dampening of the spring. Think of this as a shock absorber.
        /// </summary>
        public Single DampingConstant { get { return mFixedLinearSpring.DampingConstant; } set { mFixedLinearSpring.DampingConstant = value; } }

        /// <summary>
        /// Defines the maximum allowed value that SpringError can reach before the spring is broken.
        /// </summary>
        public Single Breakpoint { get { return mFixedLinearSpring.Breakpoint; } set { mFixedLinearSpring.Breakpoint = value; } }

        /// <summary>
        /// Creates a new instance of a FixedLinearSpring component.
        /// </summary>
        /// <param name="gameObject">The GameObject this Component is attached to.</param>
        public FixedLinearSpring(GameObject gameObject)
            : base(gameObject)
        {
            mFixedLinearSpring = new FarseerGames.FarseerPhysics.Dynamics.Springs.FixedLinearSpring();
            mSpring = mFixedLinearSpring;
        }

        /// <summary>
        /// Create the FixedLinearSpring, call this after setting any initial values.
        /// </summary>
        public override void Create()
        {
            mFixedLinearSpring = SpringFactory.Instance.CreateFixedLinearSpring(PhysicsManager.Instance.Simulator, GameObject.RigidBody.Body, BodyAttachPoint, WorldAttachPoint, SpringConstant, DampingConstant);
            mSpring = mFixedLinearSpring;
        }
    }
}
